/*
 * @(#) ShowMeetingControl
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:01:12
 */

package com.sunsharing.sharemeeting.web.controll.meeting;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunsharing.sharemeeting.service.api.MeetingService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.sunsharing.sharemeeting.entity.Meeting;
import com.sunsharing.sharemeeting.entity.ResponseModel;
import com.sunsharing.sharemeeting.entity.User;

@Controller
@RequestMapping("/meeting")
public class ShowMeetingControl {
    @Resource
    MeetingService meetingService;

    /**
     * 查询：会议详情
     * @param params {meetingId:会议Id}
     * @return
     */

    @RequestMapping(value = "/showDetail.do", method = RequestMethod.POST)
    @ResponseBody
    public String showDetail(@RequestBody JSONObject params) throws IOException {
        ResponseModel responseModel = new ResponseModel(true, "获取成功", meetingService.getMeetingDetail(params.getString("meetingId")));
        return JSONObject.toJSONString(responseModel);
    }

    /**
     * 获取用户相关会议信息列表
     * @param params {
     *              isOpen:(是否发布，1表示查询发布会议，0表示查询未发布会议（草稿）)
     *              title:（会议标题，可选，有参数的话代表会议模糊查询）
     * }
     * @return
     */
    @RequestMapping(value = "/getMeetings.do", method = RequestMethod.POST)
    @ResponseBody
    public String getMeetings(@RequestBody JSONObject params, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        User user = (User) session.getAttribute("user");
        List<Meeting> meetings = meetingService.getMeetings(
            user.getUserId(), params,user.getRoleName());
        if (meetings.size() < 10) {
            resultMap.put("meetings", meetings.subList(0, meetings.size()));//列表数据
        } else {
            resultMap.put("meetings", meetings.subList(0, 10));//列表数据
        }
        int total = meetings.size() / 10;
        if (meetings.size() % 10 != 0) {
            total++;
        }
        resultMap.put("totalPage", total);//总页数
        ResponseModel responseModel = new ResponseModel(true, "获取成功", resultMap);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 分页获取用户相关会议信息列表
     * @param params {
     *              page:要查询的页码
     *              title:会议标题（可选，有的话表示模糊分页查询，没有的话）
     *              isOpen:会议是否发布（可选，如果值为0的话表示查询草稿会议（未发布会议）的分页内容）
     * }
     * @return
     */
    @RequestMapping(value = "/getPage.do", method = RequestMethod.POST)
    @ResponseBody
    public String getMeetingByPage(@RequestBody JSONObject params,HttpSession session) {
        User user = ((User) session.getAttribute("user"));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int page = (Integer.parseInt(params.getString("page")) - 1) * 10;
        List<Meeting> meetings = meetingService.getMeetingByPage(user, page, params);
        resultMap.put("meetings", meetings);//列表数据
        ResponseModel responseModel = new ResponseModel(true, "获取成功", meetings);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 会议检索（模糊查询）
     * @param params = {
     *            title:检索的会议标题
     *        }
     * @return
     */
    @RequestMapping(value = "/searchMeeting.do", method = RequestMethod.POST)
    @ResponseBody
    public String searchMeeting(@RequestBody JSONObject params,HttpSession session) {
        User user = ((User) session.getAttribute("user"));
        ResponseModel responseModel = null;
        List<Meeting> meetings = meetingService.searchMeeting(params.getString("title"), user.getUserId());
        responseModel = new ResponseModel(true, "查询成功", meetings);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 会议备注
     * @param params = {
     *         content:会议备注内容
     *         meetingId：会议Id
     * }
     * @return
     */
    @RequestMapping(value = "/addNote.do", method = RequestMethod.POST)
    @ResponseBody
    public String addNote(@RequestBody JSONObject params,HttpSession session) {
        User user = (User) session.getAttribute("user");
        meetingService.addNote(params.getString("content"), user.getUserId(), params.getString("meetingId"));
        ResponseModel responseModel = new ResponseModel(true, "备注成功", null);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 会议进度更新
     * @param params = decisions[{
     *     decisionId:,//决议id
     *     mineId:'',//个人决议进度情况id
     *     finished:''//决议进度完成情况，最大值为100
     * }]
     * @return
     */
    @RequestMapping(value = "/changeProgress.do", method = RequestMethod.POST)
    @ResponseBody
    public String changeProgress(@RequestBody JSONObject params,HttpSession session) {
        User user = (User) session.getAttribute("user");
        meetingService.updateDecisionProgress(params, user.getUsername());
        ResponseModel responseModel = new ResponseModel(true, "更新成功", null);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 删除垃圾会议（未发布）
     * @param params {
     *          meetingId:会议Id
     * }
     * @return
     */
    @RequestMapping(value = "/delMeeting.do", method = RequestMethod.POST)
    @ResponseBody
    public String delMeeting(@RequestBody JSONObject params) {
        meetingService.delMeeting(params.getString("meetingId"));
        ResponseModel responseModel = new ResponseModel(true, "删除成功", null);
        return JSON.toJSONString(responseModel);
    }
}
